# -*- encoding=utf8 -*-
__author__ = "Administrator"

from airtest.core.api import *

auto_setup(__file__)# -*- encoding=utf8 -*-
__author__ = "易兒善"

import datetime
from airtest.core.api import *

_rc = 0
_fb_fxd = 0  
_fb_tdbk = 0
_fb_xlhj = 0

auto_setup(__file__)
_iot = 0.5
_rt = 20

def login():
    y = exists(Template(r"tpl1565016082245.png", record_pos=(-0.002, -0.522), resolution=(540, 960)))
    if y:
        touch(y)
        n = 0
        while n < 60* 5:
            l = exists(Template(r"tpl1565016531730.png", record_pos=(-0.003, 0.049), resolution=(960, 540)))
            if l:
                touch(l)
                if  exists(Template(r"tpl1565016531730.png", record_pos=(-0.003, 0.049), resolution=(960, 540))):
                    touch(l)
                

                n = 1000000
            else:
                n+=_iot
                sleep(_iot)
def closeMenu():
    gb = exists(Template(r"tpl1565012120520.png", record_pos=(0.452, -0.228), resolution=(960, 540)))
    if gb:
        touch(gb)

def close():
    closeMenu()

    gb = exists(Template(r"tpl1565015965407.png", record_pos=(0.457, -0.234), resolution=(960, 540)))
    if gb:
        touch(gb)
    gb = exists(Template(r"tpl1565017221582.png", record_pos=(0.1, 0.104), resolution=(960, 540)))
    if gb:
        touch(gb)



'''
def logout():
	close()
    m = exists(Template(r"tpl1565017422448.png", record_pos=(-0.461, 0.237), resolution=(960, 540)))
    if m:
        touch(m)
    s = exists(Template(r"tpl1565017533540.png", record_pos=(0.41, 0.167), resolution=(960, 540)))
    if s:
        touch(s)
        q = exists(Template(r"tpl1565017572504.png", record_pos=(-0.024, 0.167), resolution=(960, 540)))
        if q:
            touch(q)

'''
def gj():
    close()
    k = exists(Template(r"tpl1565103048482.png", record_pos=(0.465, 0.029), resolution=(1920, 1080)))
    if k:
        touch(k)

    k = exists(Template(r"tpl1565015827178.png", record_pos=(0.434, -0.242), resolution=(960, 540)))
    if k:
        touch(k)
        g = exists(Template(r"tpl1565015858393.png", record_pos=(-0.32, 0.195), resolution=(960, 540)))
        if g:
            touch(g)
        close()
def fh():
    ss = exists(Template(r"tpl1565265681306.png", record_pos=(-0.107, 0.085), resolution=(960, 540)))
    if ss:
        touch(ss);
## 日常任务

def rc_openMenu():
    close()
    ll = exists(Template(r"tpl1564887214187.png", record_pos=(0.411, -0.174), resolution=(960, 540)))
    if ll:
        touch(ll)

    
def rc_isEnd():
    rc_openMenu();
    r = not exists(Template(r"tpl1564888940809.png", record_pos=(-0.41, 0.036), resolution=(960, 540)))
    closeMenu()
    return r

def rc_waiteEnd(time = 20):
    sleep(20)
    q = exists(Template(r"tpl1565438033291.png", record_pos=(0.321, 0.205), resolution=(960, 540)))
    if q:
        _rc = 1
        closeMenu()
        return
    n = time * 60
    t = 0
    while n > t:
        w = exists(Template(r"tpl1565013212267.png", record_pos=(-0.309, 0.159), resolution=(960, 540)))
        if w:
            touch(w)
            sleep(_iot*5)
        k = exists(Template(r"tpl1565013401079.png", record_pos=(-0.306, 0.159), resolution=(960, 540)))
        if k:
            touch(k)
            sleep(_iot*30)
        sleep(_iot)
        t+=_iot
def rc_gogo(waitTime= 20):
    rw = exists(Template(r"tpl1565316168819.png", record_pos=(-0.445, -0.143), resolution=(960, 540)))
    if rw:
        touch(rw)
        sleep(_iot)
        xs = exists(Template(r"tpl1565316221072.png", record_pos=(-0.304, 0.022), resolution=(960, 540)))
        sleep(_iot)
        if xs:
            touch(xs)
            qw = exists(Template(r"tpl1565316253217.png", record_pos=(0.323, 0.205), resolution=(960, 540)))
            if qw:
                touch(qw)
                rc_waiteEnd(waitTime)

        else:
            rc_gogo(waitTime)
def rc_start(waitTime= 20):
    if _rc>0:
        return
    _isend = rc_isEnd()
    if _isend:
        return
    else:
        rc_gogo(waitTime)
## 单人副本任务

def fb_openMenu():
    close()
    fb= exists(Template(r"tpl1564890364683.png", record_pos=(0.471, -0.177), resolution=(960, 540)))
    if fb:
        touch(fb)

def fb_waiteEnd(timeOut=10):
	n= timeOut * 60
	t=0
	while t < n:
            lk = exists(Template(r"tpl1565314196284.png", record_pos=(0.003, 0.141), resolution=(960, 540)))
            if lk:
                touch(lk)
                return
            else:
		            sleep(_iot)
		            t+=_iot
    
def fb_isEnd(timeOut=10):
    ks = exists(Template(r"tpl1564890645890.png", record_pos=(0.298, 0.204), resolution=(960, 540)))
    if ks:
        touch(ks)
        sleep(5*_iot)
        hz = exists(Template(r"tpl1564890645890.png", record_pos=(0.298, 0.204), resolution=(960, 540)))
        if hz:
            return
        else:
            fb_waiteEnd(timeOut)
    else:
        sleep(_iot)
    fb_isEnd(timeOut)
def fb_gogo(name,time=2):
    n= 0
    while n< _rt: 
        hj = exists(name)
        if hj:
            touch(hj)
            fb_isEnd(time)
            return
        else:
            n+=1
            sleep(5*_iot)
            fb_openMenu()

def fb_tdbk(time=3):
    print(_fb_tdbk)
    if _fb_tdbk>0:
        return
    fb_gogo(Template(r"tpl1564894434122.png", record_pos=(-0.195, -0.188), resolution=(960, 540)),time)


def fb_xlhj(time=7):
    if _fb_xlhj>0:
        return
    fb_gogo(Template(r"tpl1564890501962.png", record_pos=(-0.294, -0.188), resolution=(960, 540)),time)

def fb_fsd(time=3):
    if _fb_fxd>0:
        return
    fb_gogo(Template(r"tpl1564895430108.png", record_pos=(-0.092, -0.188), resolution=(960, 540)),time)
def fb_startAll():
    fb_openMenu()
    fb_tdbk()
    fb_fsd()
    fb_xlhj()
def fb_start():
	fb_startAll()
	fb_startAll()

## 活动任务
_hd_xmzc = 0
def hd_xmzc_end():
    hd =exists(Template(r"tpl1565266335845.png", record_pos=(-0.391, -0.147), resolution=(960, 540)))
    if hd:
        return False
    else:
        return True

def hd_xmzc_waitEnd(time = 30):
    n = time*60
    t=0
    while n>t:
        if hd_xmzc_end():
            _hd_xmzc = 1
            return
        fh()
        sleep(_iot)
        t+=_iot
        
def hd_xmzc():
    if _hd_xmzc > 0:
        return
    ks = exists(Template(r"tpl1565265981063.png", record_pos=(0.002, -0.123), resolution=(960, 540)))
    if ks:
        touch(ks)
        k = exists(Template(r"tpl1565267080483.png", record_pos=(0.332, 0.195), resolution=(960, 540)))
        if k:
            touch(k)
            sleep(20)
            hd_xmzc_waitEnd()
_hd_xwxm=0 
def hd_swxm_end():
    hd =exists(Template(r"tpl1565266335845.png", record_pos=(-0.391, -0.147), resolution=(960, 540)))
    if hd:
        return False
    else:
        return True
    
def hd_swxm_waitEnd(time = 30):
    n = time*60
    t=0
    while n>t:
        if hd_swxm_end:
            _hd_xwxm = 1
            return
        fh()
        sleep(_iot)
        t+=_iot   
def hd_swxm():
    if _hd_xwxm > 0:
        return
    ks = exists(Template(r"tpl1565267468303.png", record_pos=(0.002, -0.124), resolution=(960, 540)))
    if ks:
        touch(ks)
        jr = exists(Template(r"tpl1565267664755.png", record_pos=(0.291, 0.2), resolution=(960, 540)))

        touch(jr)
        sleep()
_hd_hsmn=0
def hd_hsmn_jl():
    n = 120
    t = 0
    while n>t:
        jl = exists(Template(r"tpl1565271667529.png", record_pos=(-0.306, 0.159), resolution=(960, 540)))
        if jl:
            touch(jl)
            jx = exists(Template(r"tpl1565271804956.png", record_pos=(0.134, 0.165), resolution=(960, 540)))
            if jx:
                touch(jx)
                _hd_hsmn +=1
                return
            lk = exists(Template(r"tpl1565271947477.png", record_pos=(0.01, 0.166), resolution=(960, 540)))
            if lk:
                _hd_hsmn = 3
            
        else:
            sleep(_iot)
            t+=_iot

def hd_hsmn_hs():
    n = 60
    t = 0
    while n>t:
        hs = exists(Template(r"tpl1565271548660.png", record_pos=(-0.291, 0.156), resolution=(960, 540)))
        if hs:
            touch(hs)
            hd_hsmn_jl()
        else:
            sleep(_iot)
            t+=_iot
        
        
def hd_hsmn():
    if _hd_hsmn>2:
        return
    ks = exists(Template(r"tpl1565271392433.png", record_pos=(0.006, -0.124), resolution=(960, 540)))
    if ks:
        touch(ks)
        while _hd_hsmn<3:
            hd_hsmn_hs()

    

## boss


## 自动任务
endTime = '13:00'
starTime = '00:15'
_a_startTime = '20:00'
_a_endTime = '22:00'
def run():
    fb_start()
    rc_start()
        
    
def a_run(endT):
    while True:
        now = datetime.datetime.now()
        if now>endT:
            return
        hd_xmzc()
        hd_swxm()
        hd_hsmn()

        
    
def start():
    gj()
    while True:
        
        now = datetime.datetime.now()
        a_startTime = datetime.datetime.strptime(str(now.date())+ _a_startTime, '%Y-%m-%d%H:%M')
        a_endTime = datetime.datetime.strptime(str(now.date())+ _a_endTime, '%Y-%m-%d%H:%M')
        if now >= a_startTime and now < a_endTime:
            a_run(a_endTime)
        else:
            run()
        sleep(60)
start()


